package com.jiang.daily.Q1930;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/11/23 17:22
 */
class Solution {
    public int countPalindromicSubsequence(String s) {
        int n = s.length();
        int res = 0;
        char[] arr = s.toCharArray();

        for (char c = 'a'; c <= 'z'; c++) {
            int l = 0;
            while (l < n) {
                if (arr[l] == c) {
                    break;
                }
                l++;
            }

            int r = n - 1;
            while (r > l) {
                if (arr[r] == c) {
                    break;
                }
                r--;
            }

            Set<Character> set = new HashSet<>();
            l++;
            while(l < r) {
                if (!set.contains(arr[l])) {
                    res++;
                    set.add(arr[l]);
                }
                l++;
            }
        }
        return res;
    }
}
